/**
 * @作者 chengxinxiaofeng
 * @时间 2019年7月15日10:30:20
 */
package com.chinaunicom.tour.sys.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.chinaunicom.tour.sys.constant.ResultConstants;
import com.chinaunicom.tour.sys.entity.AirplanTicket;
import com.chinaunicom.tour.sys.entity.BuyAirplanTicket;
import com.chinaunicom.tour.sys.entity.User;
import com.chinaunicom.tour.sys.model.AirplanTicketModel;
import com.chinaunicom.tour.sys.service.AirplanTicketService;
import com.chinaunicom.tour.sys.service.BuyAirplanTicketService;
import com.chinaunicom.tour.sys.util.BaseResponse;
import com.chinaunicom.tour.sys.util.ObjectRestResponse;
import com.chinaunicom.tour.sys.util.TableResultResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "机票管理", tags = { "机票管理" })
@RestController
@RequestMapping("airplanTicket")
public class AirplanTicketController {

	private static final BaseResponse orr = null;

	@Autowired
	AirplanTicketService airplanTicketService;
	
	@Autowired
	BuyAirplanTicketService buyAirplanTicketService;
	
    @ApiOperation(value = "购买机票", notes = "")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<BuyAirplanTicket> add(@RequestBody @Validated BuyAirplanTicket entity,
    		BindingResult results, HttpServletRequest request) {
        ObjectRestResponse<BuyAirplanTicket> orr = new ObjectRestResponse<BuyAirplanTicket>();
        orr.setData(null);
        try {
        	//不为空校验为空
            if (results.hasErrors()) {
                orr.setStatus(ResultConstants.ERROR_CODE);
                orr.setMessage(results.getFieldError().getDefaultMessage());
                return orr;
            }
            
            Object sessionObJ = request.getSession().getAttribute("user");
            //session不存在 或 用户id不存在
            if(sessionObJ == null || ((User)sessionObJ).getId() == null ){
            	 orr.setStatus(ResultConstants.ERROR_CODE);
                 orr.setMessage("购买机票失败，获取用户信息失败...");
             //获取session成功
            }else{
            	User user = (User)sessionObJ;
            	//购买用户ID
            	entity.setUserId(user.getId());
                Boolean result =  buyAirplanTicketService.add(entity);
                //成功
                if(result){
                    orr.setStatus(ResultConstants.SUCCESS_CODE);
                    orr.setMessage("购买机票成功");
                //失败
                }else{
                    orr.setStatus(ResultConstants.ERROR_CODE);
                    orr.setMessage("购买机票失败，航班不存在或余票不足...");
                }
            }
            return orr;
        } catch (Exception e) {
            e.printStackTrace();
            orr.setStatus(ResultConstants.ERROR_CODE);
            orr.setMessage("购买机票失败" + e.getMessage());
            return orr;
        }
    }
	
    @ApiOperation(value = "分页查询航班信息", notes = "")
    @RequestMapping(value = "/pageQuery", method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<AirplanTicket> pageQuery(@ModelAttribute AirplanTicketModel queryModel) {
        TableResultResponse<AirplanTicket> trr = new TableResultResponse<AirplanTicket>();

        try {
            List<AirplanTicket> list = airplanTicketService.pageQuery(queryModel);
            trr.getData().setTotal(airplanTicketService.pageQueryCount(queryModel));
            trr.getData().setRows(list);
            trr.setStatus(ResultConstants.SUCCESS_CODE);
            trr.setMessage("查询成功");
            return trr;
        } catch (Exception e) {
            e.printStackTrace();
            trr.setStatus(ResultConstants.ERROR_CODE);
            trr.setMessage("查询失败" + e.getMessage());
            trr.getData().setTotal(0);
            trr.getData().setRows(null);
            return trr;
        }
    }

    @ApiOperation(value = "查询所有航班信息", notes = "")
    @RequestMapping(value = "/findList", method = RequestMethod.POST)
    @ResponseBody
    public TableResultResponse<AirplanTicket> findList(@RequestBody AirplanTicketModel queryModel) {
        TableResultResponse<AirplanTicket> trr = new TableResultResponse<AirplanTicket>();

        try {
            List<AirplanTicket> list = airplanTicketService.findList(queryModel);
            trr.getData().setTotal(list.size());
            trr.getData().setRows(list);
            trr.setStatus(ResultConstants.SUCCESS_CODE);
            trr.setMessage("查询成功");
            return trr;
        } catch (Exception e) {
            e.printStackTrace();
            trr.setStatus(ResultConstants.ERROR_CODE);
            trr.setMessage("查询失败" + e.getMessage());
            trr.getData().setTotal(0);
            trr.getData().setRows(null);
            return trr;
        }
    }

    @ApiOperation(value = "根据航班id查询航班信息", notes = "")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<AirplanTicket> findById(@PathVariable String id) {
        ObjectRestResponse<AirplanTicket> orr = new ObjectRestResponse<AirplanTicket>();

        try {
        	AirplanTicket airplanTicket = airplanTicketService.findById(id);
            orr.setData(airplanTicket);
            orr.setStatus(ResultConstants.SUCCESS_CODE);
            orr.setMessage("查询成功");
            return orr;
        } catch (Exception e) {
            e.printStackTrace();
            orr.setStatus(ResultConstants.ERROR_CODE);
            orr.setMessage("查询失败" + e.getMessage());
            return orr;
        }
    }
    
    @ApiOperation(value = "查询当前登录用户购买机票列表", notes = "")
    @RequestMapping(value = "/findListByUserId", method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<BuyAirplanTicket> findListByUserId(HttpServletRequest request) {
    	TableResultResponse<BuyAirplanTicket> trr = new TableResultResponse<BuyAirplanTicket>();

        try {
        	 Object sessionObJ = request.getSession().getAttribute("user");
        	  
            if(sessionObJ == null || ((User)sessionObJ).getId() == null ){
            	trr.setStatus(ResultConstants.ERROR_CODE);
            	trr.setMessage("查询失败，获取用户信息失败...");
                return trr;
            //获取session成功
           }else{
        	   List<BuyAirplanTicket> list = airplanTicketService.findListByUserId(((User)sessionObJ).getId());
               trr.getData().setTotal(list.size());
               trr.getData().setRows(list);
               trr.setStatus(ResultConstants.SUCCESS_CODE);
               trr.setMessage("查询成功");
           }
            return trr;
        } catch (Exception e) {
            e.printStackTrace();
            trr.setStatus(ResultConstants.ERROR_CODE);
            trr.setMessage("查询失败" + e.getMessage());
            return trr;
        }
    }
    
    @ApiOperation(value = "根据机票ID查询购买机票信息", notes = "")
    @RequestMapping(value = "/findTicketInfoById", method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<BuyAirplanTicket> findTicketInfoById(String ticketId) {
    	ObjectRestResponse<BuyAirplanTicket> orr = new ObjectRestResponse<BuyAirplanTicket>();

        try {
        	BuyAirplanTicket buyAirplanTicket= airplanTicketService.findTicketInfoById(ticketId);
        	orr.setData(buyAirplanTicket);
        	orr.setStatus(ResultConstants.ERROR_CODE);
        	orr.setMessage("查询成功");
            return orr;
        } catch (Exception e) {
            e.printStackTrace();
            orr.setStatus(ResultConstants.ERROR_CODE);
            orr.setMessage("查询失败" + e.getMessage());
            return orr;
        }
    }
    
}
